/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.plugins;

import com.cxhd.pacs.tools.MysqlDbTool;
import com.cxhd.pacs.tools.SqlServerDbTool;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author 52461
 */
public class LisDataPlugin implements BasePlugin {

    public String pluginName = "plugin_lis_data";
    //private MysqlDbTool con = null;
    private SqlServerDbTool con = null;
    private List<Map<String, String>> data = null;
    private String patient_code;
    private String sample_name;
    private String patient_name;
    private String patient_age;
    private String patient_sex;

    public LisDataPlugin() {

    }

    public void connect() {
        // con = new MysqlDbTool("localhost", "root", "efotoadmin", "lis");
        con = new SqlServerDbTool("1.255.255.93:1555", "sa", "7system!", "yh_sdzyy");
        con.sqlServerConnect();
        // con.mysqlConnect();
        //SqlServerDbTool con = new SqlServerDbTool();  
    }

    @Override
    public void start() {
        connect();
        doService();
        disconnect();
    }

    public void disconnect() {
       // con.mysqlDisconnect();
       con.sqlServerDisconnect();
        data = null;
        patient_code = null;
        sample_name = null;
    }

    public LisDataPlugin setData(List<Map<String, String>> data, String patient_code, String sample_name, String patient_name, String patient_age, String patient_sex) {

        this.data = data;
        this.patient_code = patient_code;
        this.sample_name = sample_name;
        this.patient_name = patient_name;
        this.patient_age = patient_age;
        this.patient_sex = patient_sex;

        return this;
    }

    public void doService() {
        try {
            for (Map<String, String> map : data) {
                map.put("patient_code", patient_code);
                map.put("sample_name", sample_name);
                map.put("patient_name", patient_name);
                map.put("patient_age", patient_age);
                map.put("patient_sex", patient_sex);

                makeInsertSqlInsert(map, "lis_detail");
            }
            //makeInsertSqlInsert(data, "lis_detail");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private boolean makeInsertSqlInsert(Map<String, String> map, String tableName) throws Exception {

        if (map == null) {
            return false;
        }

        String sql = "";
        StringBuilder sqlHead = new StringBuilder();
        StringBuilder sqlvalue = new StringBuilder();
        int index = 0;
        String[] values = new String[map.size()];
        sqlHead.append("insert into ").append(tableName).append(" (");
        sqlvalue.append(" values (");
        for (String key : map.keySet()) {
            values[index] = map.get(key);
            index++;
            if (index == map.size()) {
                sqlHead.append("`").append(key).append("`").append(" )");
                sqlvalue.append(map.get(key) == null ? "null)" : "'" + map.get(key) + "')");
                // map.get(key)==null?"":"";
                // sqlvalue.append("? )");
            } else {
                sqlHead.append("`").append(key).append("`").append(" , ");
                sqlvalue.append(map.get(key) == null ? "null," : "'" + map.get(key) + "',");
                //sqlvalue.append("?,");
            }

        }
        sql = sqlHead.append(sqlvalue).toString();
        // System.out.println(sql);
        //logger.info(sql);
        con.sqlServerSetQ(sql);
        return true;

    }

}
